package com.hrzyit.whistle.im.ui.adapter

import android.content.Context
import android.os.Handler
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.hrzyit.whistle.im.common.IMConstants
import com.hrzyit.whistle.im.entity.MessageInfo
import com.hrzyit.whistle.im.ui.adapter.holder.ChatAcceptViewHolder
import com.hrzyit.whistle.im.ui.adapter.holder.ChatSendViewHolder
import com.jude.easyrecyclerview.adapter.BaseViewHolder
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter

class ChatAdapter(context: Context) : RecyclerArrayAdapter<MessageInfo>(context) {

    private var holderOnItemClickListener: OnItemClickListener? = null
    var handler: Handler = Handler()

    override fun OnCreateViewHolder(parent: ViewGroup?, viewType: Int): BaseViewHolder<MessageInfo> {
        var viewHolder: BaseViewHolder<MessageInfo>? = null
        when (viewType) {
            IMConstants.CHAT_ITEM_TYPE_LEFT -> viewHolder =
                ChatAcceptViewHolder(parent!!, holderOnItemClickListener!!, handler)
            IMConstants.CHAT_ITEM_TYPE_RIGHT -> viewHolder =
                ChatSendViewHolder(parent!!, holderOnItemClickListener!!, handler)
        }
        return viewHolder!!
    }

    override fun getViewType(position: Int): Int {
        return (allData[position] as MessageInfo).type
    }

    fun addItemClickListener(onItemClickListener: OnItemClickListener) {
        this.holderOnItemClickListener = onItemClickListener
    }

    interface OnItemClickListener {
        fun onHeaderClick(position: Int)

        fun onImageClick(view: View, position: Int)

        fun onVoiceClick(imageView: ImageView, position: Int)
    }
}
